package cn.regexp.mybatisinterceptordemo.config;

import cn.regexp.mybatisinterceptordemo.interceptor.SqlInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.List;


@Configuration
public class MyBatisConfig {

    // SqlSessionFactory 用于创建 SqlSession 对象
    @Autowired
    private List<SqlSessionFactory> sqlSessionFactoryList;

    // @PostConstruct 对象创建后立即执行，且仅执行一次，这里用该注解为 SqlSessionFactory 添加 SQL拦截器
    @PostConstruct
    public void addMySqlInterceptor() {
        SqlInterceptor interceptor = new SqlInterceptor();
        for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
            sqlSessionFactory.getConfiguration().addInterceptor(interceptor);
        }
    }
}